/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 16:19:19
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-04 09:21:04
 * @FilePath: \cxl-driving-training\src\views\price\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { priceEdit, priceList, priceOperationInterface, pricePageInterface } from '@/apis/price'
import { DICT_DATA, optionsInterface } from '@/utils/dict-data'

export default class CompanyAdminData extends BaseData implements BaseMethod {
    dialog = reactive({
        visible: false,
        title: '设置价格',
        width: '800px'
    })
    dialogForm = reactive({
        type: '',
        class: '',
        activePrice: '',
        originalPrice: '',
        productId: '',
        id: '',
        businessId: '',
        activePricec1: '',
        originalPricec1: '',
        id_c1: '',
        businessId_c1: '',
        activePricec2: '',
        originalPricec2: '',
        id_c2: '',
        businessId_c2: '',
        vehicleType: 0
    })
    rules = {
        type: [{ required: true, message: '请选择学车类型', trigger: 'change' }],
        class: [{ required: true, message: '请选择班别类型', trigger: 'change' }],
        activePrice: [{ required: true, message: '请输入活动价', trigger: 'blur' }],
        originalPrice: [{ required: true, message: '请输入原价', trigger: 'blur' }],
        activePricec1: [{ required: true, message: '请输入活动价', trigger: 'blur' }],
        originalPricec1: [{ required: true, message: '请输入原价', trigger: 'blur' }],
        activePricec2: [{ required: true, message: '请输入活动价', trigger: 'blur' }],
        originalPricec2: [{ required: true, message: '请输入原价', trigger: 'blur' }]
    }
    //学车类型
    typeOptions = reactive<optionsInterface[]>([...DICT_DATA.get('vehicleType')])
    //班别类型
    classOptions = reactive<optionsInterface[]>([...DICT_DATA.get('classType')])
    searchData = { classType: '', vehicleType: '', status: '' }
    constructor() {
        super(tableConfig)
        this.getData()
    }
    getData = (arg?: any) => {
        const params: pricePageInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        if (this.searchData.status !== '' && this.searchData.status !== '-1') {
            params.disable = Number(this.searchData.status)
        }
        if (this.searchData.vehicleType !== '' && this.searchData.vehicleType !== '-1') {
            params.vehicleType = Number(this.searchData.vehicleType)
        }
        if (this.searchData.classType !== '' && this.searchData.classType !== '-1') {
            params.classType = Number(this.searchData.classType)
        }
        priceList(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total

                const arr: any[] = res.data.result.records

                arr.forEach((element: any) => {
                    element.status = Number(element.disable) === 0 ? true : false
                    element.originalPrice = ''
                    element.activePrice = ''
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchData = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchData = { classType: '', vehicleType: '', status: '' }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'set') {
            this.dialog.visible = true
            this.dialogForm.type = data.vehicleType + ''
            this.dialogForm.class = data.classType + ''
            this.dialogForm.productId = data.id
            if (data.productPriceVOS.length) {
                data.productPriceVOS.forEach((item: any) => {
                    if (item.type === 1) {
                        //C1
                        this.dialogForm.id_c1 = item.id
                        this.dialogForm.originalPricec1 = item.originalPrice
                        this.dialogForm.activePricec1 = item.activePrice
                    } else if (item.type === 2) {
                        //C2
                        this.dialogForm.id_c2 = item.id
                        this.dialogForm.originalPricec2 = item.originalPrice
                        this.dialogForm.activePricec2 = item.activePrice
                    } else {
                        this.dialogForm.id = item.id
                        this.dialogForm.originalPrice = item.originalPrice
                        this.dialogForm.activePrice = item.activePrice

                        this.dialogForm.businessId = data.businessId
                    }
                })
            } else {
                this.dialogForm.businessId = data.businessId
            }
        }
    }
    //监听弹窗关闭
    onCloseDialog = (formEl: FormInstance | undefined) => {
        if (!formEl) return
        formEl.resetFields()
        this.dialog.visible = false
    }
    //监听弹窗点击确定按钮
    submitForm = async (formEl: FormInstance | undefined) => {
        if (!formEl) return
        await formEl.validate((valid, fields) => {
            if (valid) {
                console.log('submit!', this.dialogForm)
                const priceArray: priceOperationInterface[] = []
                if (Number(this.dialogForm.type) === 1) {
                    //学车类型 小车
                    priceArray.push({
                        activePrice: this.dialogForm.activePricec1,
                        originalPrice: this.dialogForm.originalPricec1,
                        priceId: this.dialogForm.id_c1,
                        productId: this.dialogForm.productId,
                        type: 1,
                        businessId: this.dialogForm.businessId
                    })
                    priceArray.push({
                        activePrice: this.dialogForm.activePricec2,
                        originalPrice: this.dialogForm.originalPricec2,
                        priceId: this.dialogForm.id_c2,
                        productId: this.dialogForm.productId,
                        type: 2,
                        businessId: this.dialogForm.businessId
                    })
                } else {
                    priceArray.push({
                        activePrice: this.dialogForm.activePrice,
                        originalPrice: this.dialogForm.originalPrice,
                        priceId: this.dialogForm.id,
                        productId: this.dialogForm.productId,
                        businessId: this.dialogForm.businessId
                    })
                }
                console.log(priceArray)
                priceEdit(priceArray).then(res => {
                    if (Number(res.data.code) === 200) {
                        this.dialog.visible = false
                        this.getData()
                        ElMessage.success('修改成功')
                    }
                })
            } else {
                console.log('error submit!', fields)
            }
        })
    }
}
